<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\controller\adminapi\platform\douyin;

use app\controller\adminapi\platform\PlatformController;
use think\App;
use app\common\model\platform\Platform;
use hiduoke\services\FormBuilder as Form;

/**
 * Class WorkWechat 抖音
 * @package app\controller\adminapi\platform\douyin
 * User: liuhongbin
 * Date: 2022-07-17 17:25
 */
class Douyin extends PlatformController
{
    /**
     * @var Platform
     */
    public $model;

    public function __construct(App $app, Platform $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 显示抖音列表
     * @Route("wechat", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        //
        return $this->success("ok");
    }

    /**
     * 显示创建抖音表单页.
     * @Route("wechat/create", method="GET")
     * @return \think\Response
     */
    public function create()
    {
        return $this->getForm(0);
    }

    /**
     * 保存新建的抖音
     * @Route("wechat", method="POST")
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save()
    {
        return $this->saveForm(0);
    }

    /**
     * 显示编辑抖音表单页.
     * @Route("wechat/id/edit", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        return $this->getForm($id);
    }

    private function getForm($id)
    {
        $rule = array();
        //创建表单
        $row = $this->model->where(['plaid' => $id])->findOrEmpty();
        if ($row->isEmpty()) {
            //新增
            $form = Form::createForm("/adminapi/platform/douyin/index")->setMethod("POST");
            $title = "新增抖音平台";
        } else {
            $form = Form::createForm("/adminapi/platform/douyin/index/" . $id)->setMethod("PUT");
            $title = "编辑抖音平台";
        }
        //新增表单
        $rule[] = Form::hidden('type', 4);
        $rule[] = Form::number('sort', '排序', $row->sort ?? '');
        $rule[] = Form::input('name', '应用名称', $row['name'] ?? '')->required();
        $rule[] = Form::dkInput('config_client_key', '应用Key', $row['config']['client_key'] ?? '', '在开放平台中-管理中心-应用管理-网站应用-详情')->required();
        $rule[] = Form::dkInput('config_client_secret', '应用Secret', $row['config']['client_secret'] ?? '', '在开放平台中-管理中心-应用管理-网站应用-详情')->required();
        $rule[] = Form::dkInput('config_domain', '授权域回调', $row['config']['domain'] ?? '', "在开发平台中-管理中心-应用管理-网站应用-详情-基础信息");
        $rule[] = Form::selectMultiple('config_scopes', '选择权限',$row['config']['scopes']??[])
            ->options($this->getAuthOptions())
            ->clearable(true)
            ->filterable(true)
            ->allowCreate(true)
            ->effect(['help'=>'设置应用授权登录的时候需要的权限，如果未开通，授权将会失败']);
        $rule[] = Form::input('description', '描述', $row->description ?? '');
        $rule[] = Form::dkImage('logo', '图标', $row->logo ?? '')->appendToBody();

        // 权限管理

        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    private function saveForm($id)
    {
        $wallet = $this->userInfo['wallet'];
        if (!$id && $wallet['douyin'] <= $wallet['use_count']['douyin']) {
            return $this->error("创建次数不足");
        }
        $formData = getFormData();
        if (!$id) {
            $formData['uid'] = $this->userInfo['uid'];
        }
        $row = $this->model->findOrEmpty($id);
        $row->save($formData);
        return $this->success("提交成功", ['plaid' => $row->plaid]);
    }

    /**
     * 保存更新的抖音
     * @Route("wechat/id", method="PUT")
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update($id)
    {
        return $this->saveForm($id);
    }

    private function getAuthOptions()
    {
        return [
            ['label' => '分享给抖音好友/群(im.share)', 'value' => 'im.share'],
            ['label' => '发布内容至抖音：APP场景(aweme.share)', 'value' => 'aweme.share'],
            ['label' => '发布内容至抖音：H5场景(h5.share)', 'value' => 'h5.share'],
            ['label' => '使用抖音拍摄器(aweme.capture)', 'value' => 'aweme.capture'],
            ['label' => '视频查询(video.data)', 'value' => 'video.data'],
            ['label' => '视频查询(video.list)', 'value' => 'video.list'],
            ['label' => '关键词视频管理(video.search)', 'value' => 'video.search'],
            ['label' => '关键词视频管理(video.search.comment)', 'value' => 'video.search.comment'],
            ['label' => '授权登录与用户基础信息(user_info)', 'value' => 'user_info'],
            ['label' => '关注和粉丝列表(following.list)', 'value' => 'following.list'],
            ['label' => '关注和粉丝列表(fans.list)', 'value' => 'fans.list'],
            ['label' => '(fans.check)', 'value' => 'fans.check'],
            ['label' => '授权动态续期(renew_refresh_token)', 'value' => 'renew_refresh_token'],
            ['label' => '获取用户手机号(mobile_alert)', 'value' => 'mobile_alert'],
            ['label' => '获取用户手机号(mobile)', 'value' => 'mobile'],
            ['label' => '评论管理（普通用户）(item.comment)', 'value' => 'item.comment'],
            ['label' => '外部用户数据(data.external.user)', 'value' => 'data.external.user'],
            ['label' => '视频数据(data.external.item)', 'value' => 'data.external.item'],
            ['label' => '粉丝画像数据(fans.data)', 'value' => 'fans.data'],
            ['label' => '抖音影视综榜单数据(discovery.ent)', 'value' => 'discovery.ent'],
            ['label' => 'SDK分享视频数据(data.external.sdk_share)', 'value' => 'data.external.sdk_share'],
            ['label' => '粉丝喜好数据（粉丝喜好、粉丝热评）(data.external.fans_favourite)', 'value' => 'data.external.fans_favourite'],
            ['label' => '粉丝来源数据(data.external.fans_source)', 'value' => 'data.external.fans_source'],
            ['label' => '查询POI地点信息(poi.search)', 'value' => 'poi.search'],
            ['label' => '获取openTicket(open.get.ticket)', 'value' => 'open.get.ticket'],
            ['label' => '一次性订阅消息(message.once.send)', 'value' => 'message.once.send'],
            ['label' => 'CPS佣金设置与查询(poi.cps.common)', 'value' => 'poi.cps.common'],
            ['label' => '查询小程序挂载权限(micapp.is_legal)', 'value' => 'micapp.is_legal'],
            ['label' => '服务市场服务订购相关(market.service.user)', 'value' => 'market.service.user'],
            ['label' => '增量授权(incremental_authorization)', 'value' => 'incremental_authorization'],
            ['label' => '签名ticket(js.ticket)', 'value' => 'js.ticket'],
            ['label' => '跳转用户授权页面(jsb.open.auth)', 'value' => 'jsb.open.auth'],
            ['label' => '唤起用户授权界面(jsb.open.showAuth)', 'value' => 'jsb.open.showAuth'],
            ['label' => '用原生能力后台播放音乐(jsb.open.music)', 'value' => 'jsb.open.music'],
            ['label' => '控制webview全屏手势(jsb.app.webviewControl)', 'value' => 'jsb.app.webviewControl'],
            ['label' => '在抖音端内收藏抖音music clip(jsb.app.musicCollect)', 'value' => 'jsb.app.musicCollect'],
            ['label' => '抖音端内查询音乐收藏状态(jsb.app.musicCollectStatus)', 'value' => 'jsb.app.musicCollectStatus']
        ];
    }

}
